package cn.gaily.eduorder.controller;


import cn.gaily.commonutils.R;
import cn.gaily.eduorder.service.TPayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author xuchengwei
 * @since 2021-08-09
 */
@Api(description = "微信支付管理")
@RestController
@RequestMapping("/eduorder/paylog")
@CrossOrigin
public class TPayLogController {

    @Autowired
    private TPayLogService logService;

    @ApiOperation(value = "生成二维码接口")
    @PostMapping("createNative/{orderNo}")
    public R createQRcode(@PathVariable String orderNo){
        Map<String,Object> map = logService.createQRcode(orderNo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "查询订单支付状态接口")
    @PostMapping("queryPayStatus/{orderNo}")
    public R getOrderStatus(@PathVariable String orderNo){
        Map<String,String> map = logService.queryPayStatus(orderNo);
        if(map == null){
            return R.error().message("支付出错");
        }
        if(map.get("trade_state").equals("SUCCESS")){
            boolean res = logService.updateOrderStatus(map);
            if(res){
                return R.ok().message("支付成功");
            }
        }
        return R.ok().code(25000).message("订单支付中");
    }
}

